package com.huiquan.foundation.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huiquan.analysis.domain.User;
import com.huiquan.foundation.service.TabHelpService;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;

@Controller
public class TabHelpController {

	protected final Logger LOGGER = LoggerFactory.getLogger(getClass());

	@Autowired
	private TabHelpService tabHelpService;

	/**
	 * 功能测试 类型type：initzs-；sortzs-；
	 */
	@ResponseBody
	@RequestMapping(value = "/tabHelp.do")
	public ReturnData list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		@SuppressWarnings("unchecked")
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		User user = (User) req.getSession().getAttribute("user");
		if (pageParam != null) {
			String tabId = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "tabid");
			if (StringUtils.isNotBlank(tabId)) {
				Map<String, Object> rtnMap = tabHelpService.list(tabId,user);
				return ReturnUtil.success(rtnMap);
			}
		}
		ReturnCode rc = ReturnCode.ELEMENT_CHECK_PAGE_ERROR;
		return ReturnUtil.fail(rc);
	}

	/**
	 * 功能测试 类型type：initzs-；sortzs-；
	 */
	@ResponseBody
	@RequestMapping(value = "/tabHelpChange.do")
	public ReturnData change(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		@SuppressWarnings("unchecked")
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		User user = (User) req.getSession().getAttribute("user");
		String newRemark = req.getParameter("remark");
		if (pageParam != null) {
			String tabId = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "tabid");
			Map<String, Object> map = new HashMap<>();
			if (StringUtils.isNotBlank(tabId) ) {
				tabHelpService.change(tabId, newRemark, user);
				map.put("remark", newRemark);
			}
			return ReturnUtil.success(map);
		}else{
			ReturnCode rc = ReturnCode.ELEMENT_CHECK_PAGE_ERROR;
			rc.setDesc("无法启用帮助");
			return ReturnUtil.fail(rc);
		}
	}

}
